<template>
  <div class="login-container">
    <!-- 左侧品牌区域 -->
    <div class="login-brand">
      <div class="brand-content">
        <div class="brand-icon">
          <el-icon :size="80"><School /></el-icon>
        </div>
        <h1 class="brand-title">学生档案管理系统</h1>
        <p class="brand-subtitle">Student Records Management System</p>
        <div class="brand-features">
          <div class="feature-item">
            <el-icon><Document /></el-icon>
            <span>档案管理</span>
          </div>
          <div class="feature-item">
            <el-icon><DataAnalysis /></el-icon>
            <span>数据分析</span>
          </div>
          <div class="feature-item">
            <el-icon><User /></el-icon>
            <span>学生信息</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录区域 -->
    <div class="login-form-wrapper">
      <div class="login-box">
        <div class="login-header">
          <h2 class="login-title">欢迎登录</h2>
          <p class="login-desc">请输入您的账号密码进行登录</p>
        </div>

        <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
          <el-form-item prop="username">
            <div class="input-label">用户名</div>
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="large"
              clearable
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <div class="input-label">密码</div>
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              show-password
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleLogin"
              class="login-button"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>


      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, FormInstance } from 'element-plus'
import { School, Document, DataAnalysis, User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/auth'
import { useUserStore } from '@/store/user'
import type { LoginForm } from '@/types'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive<LoginForm>({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const data = await login(loginForm)
        userStore.login(data)
        ElMessage.success('登录成功')
        router.push('/')
      } catch (error) {
        console.error('登录失败', error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

// 左侧品牌区域
.login-brand {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  position: relative;
  overflow: hidden;

  // 装饰圆圈
  &::before,
  &::after {
    content: '';
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
  }

  &::before {
    width: 400px;
    height: 400px;
    top: -100px;
    right: -100px;
  }

  &::after {
    width: 300px;
    height: 300px;
    bottom: -80px;
    left: -80px;
  }

  .brand-content {
    position: relative;
    z-index: 1;
    text-align: center;
    color: white;

    .brand-icon {
      margin-bottom: 32px;
      
      .el-icon {
        background: rgba(255, 255, 255, 0.2);
        padding: 24px;
        border-radius: 50%;
        backdrop-filter: blur(10px);
      }
    }

    .brand-title {
      margin: 0 0 16px 0;
      font-size: 36px;
      font-weight: 600;
      letter-spacing: 2px;
    }

    .brand-subtitle {
      margin: 0 0 48px 0;
      font-size: 16px;
      opacity: 0.9;
      letter-spacing: 1px;
    }

    .brand-features {
      display: flex;
      gap: 40px;
      justify-content: center;

      .feature-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 12px;
        opacity: 0.95;

        .el-icon {
          font-size: 32px;
          background: rgba(255, 255, 255, 0.2);
          padding: 16px;
          border-radius: 12px;
          backdrop-filter: blur(10px);
        }

        span {
          font-size: 14px;
          font-weight: 500;
        }
      }
    }
  }
}

// 右侧登录区域
.login-form-wrapper {
  width: 520px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.05);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 0 40px;
}

.login-header {
  margin-bottom: 40px;
  text-align: center;

  .login-title {
    margin: 0 0 12px 0;
    font-size: 28px;
    font-weight: 600;
    color: #303133;
  }

  .login-desc {
    margin: 0;
    font-size: 14px;
    color: #909399;
  }
}

.login-form {
  .input-label {
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
    color: #606266;
  }

  :deep(.el-form-item) {
    margin-bottom: 24px;
  }

  :deep(.el-input__wrapper) {
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: 0 0 0 1px #dcdfe6 inset;
    transition: all 0.3s;

    &:hover {
      box-shadow: 0 0 0 1px #c0c4cc inset;
    }

    &.is-focus {
      box-shadow: 0 0 0 1px #409eff inset;
    }
  }

  :deep(.el-input__prefix) {
    display: flex;
    align-items: center;
    color: #909399;
    font-size: 16px;
  }

  .login-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    border-radius: 8px;
    margin-top: 8px;
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
    border: none;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 20px rgba(79, 172, 254, 0.4);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

.login-tips {
  margin-top: 32px;

  :deep(.el-alert) {
    border-radius: 8px;
    background: #f0f9ff;
    border: 1px solid #b3d8ff;

    .el-alert__title {
      font-size: 13px;
      font-weight: 500;
      color: #409eff;
    }

    .el-alert__content {
      margin-top: 8px;
    }
  }

  .tip-content {
    p {
      margin: 6px 0;
      font-size: 13px;
      color: #606266;
      line-height: 1.6;
    }
  }
}

.login-footer {
  margin-top: 32px;
  text-align: center;
  
  span {
    font-size: 12px;
    color: #c0c4cc;
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .login-brand {
    display: none;
  }

  .login-form-wrapper {
    width: 100%;
  }
}
</style>

